'''CopyRight WangShiheng(C) 2021'''
import winreg
import _read
import os

def save_reg(toolcfg_name):
    #read_cfg
    with open(toolcfg_name) as f:
        text = f.read()
    cfg = _read.read_json(text)

    #save_reg
    reg = {
        'main_path':'SOFTWARE\WangShiheng\SmallTools\Tools'
    }
    reg['main_key'] = winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER,f'SOFTWARE\WangShiheng\SmallTools\Tools\{cfg["file_name"][:-4]}'
                                        ,reserved=0,access=winreg.KEY_ALL_ACCESS)
    for key in cfg.keys():
        if key != 'file_path' & key != 'dir':
            winreg.SetValueEx(reg['main_key'],key,0,winreg.REG_SZ,cfg[key])
        else:
            winreg.SetValueEx(reg['main_key'],key,0,winreg.REG_SZ,os.path.join(os.getcwd(),cfg['file_path']))
    winreg.FlushKey(reg['main_key'])
    winreg.CloseKey(reg['main_key'])
def _FindReg(toolName):
    pass
def SaveCoreReg(cfg):
    reg = winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER,f'SOFTWARE\WangShiheng\SmallTools\Core',reserved=0,access=winreg.KEY_ALL_ACCESS)
    for key in cfg.keys():
        if key == 'CoreVersion':
            winreg.SetValueEx(reg,'CoreVersionNumber',0,winreg.REG_SZ,cfg[key][1])
            winreg.SetValueEx(reg,'CoreVersionEdition',0,winreg.REG_SZ,cfg[key][0])
        else:
            winreg.SetValueEx(reg,key,0,winreg.REG_SZ,cfg[key])
    winreg.FlushKey(reg)
    winreg.CloseKey(reg)

if __name__ == '__main__':
    os.chdir('E:\Python\project\SmallTools\SourceCode\SourceCode\dist\ComputerConfig')
    save_reg('ComputerConfig')